package com.hxgis.oc.cma.util;

import cn.hutool.http.HttpUtil;
import com.hxgis.oc.config.CmaConfig;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class CmaUrlGenerator {
    
    protected static final String SERVER_ID = "NMIC_MUSIC_CMADAAS";

    private static String createSign(Map<String, Object> baseParams)
    {
        String musicPassword = CmaConfig.getConfig().getMusicPassword();
        HashMap<String, Object> tmpQueryParam = new HashMap<>(baseParams);
        tmpQueryParam.put("pwd", musicPassword);
        return CmaSignGenerator.createSign(tmpQueryParam);
    }

    private static Map<String, Object> getBaseQueryParams() {
        String musicUsername = CmaConfig.getConfig().getMusicUsername();
        Map<String, Object> baseParams = new HashMap<>(4);
        baseParams.put("serviceNodeId", SERVER_ID);
        baseParams.put("userId", musicUsername);
        baseParams.put("timestamp", System.currentTimeMillis());
        baseParams.put("nonce", UUID.randomUUID().toString());
        return baseParams;
    }

    public static String generateFileUrl(String furtive) {
        Map<String, Object> baseParams = getBaseQueryParams();
        baseParams.put("url", furtive);
        baseParams.put("sign", createSign(baseParams));

        String baseUrl = CmaConfig.getConfig().getBaseUrl() + "/file";
        return HttpUtil.urlWithForm(baseUrl,baseParams, StandardCharsets.UTF_8, false);
    }

    public static String generateJsonUrl(Map<String, Object> requestParams,String InterfaceId) {
        Map<String, Object> baseParams = getBaseQueryParams();
        baseParams.putAll(requestParams);
        baseParams.put("dataFormat", "json");
        baseParams.put("interfaceid", InterfaceId);
        baseParams.put("sign", createSign(baseParams));
        String baseUrl = CmaConfig.getConfig().getBaseUrl() + "/api";
        return HttpUtil.urlWithForm(baseUrl, baseParams, StandardCharsets.UTF_8, false);
    }
}
